import { Router } from 'express';
import { body, param, query } from 'express-validator';
import { ActivityController } from '../controllers/activityController';
import { authenticate, authorize } from '../../middleware/auth';
import { validateRequest } from '../../middleware/validation';
import { ALL_PERMISSIONS } from '../../constants/permissions';

const router = Router();
const activityController = new ActivityController();

/**
 * @swagger
 * tags:
 *   name: 活动管理
 *   description: 活动管理相关接口
 */

/**
 * @swagger
 * /api/admin/activities:
 *   post:
 *     summary: 创建活动
 *     tags: [活动管理]
 *     security:
 *       - bearerAuth: []
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             required:
 *               - name
 *               - startTime
 *               - endTime
 *             properties:
 *               name:
 *                 type: string
 *                 description: 活动名称
 *               description:
 *                 type: string
 *                 description: 活动描述
 *               startTime:
 *                 type: string
 *                 format: date-time
 *                 description: 开始时间
 *               endTime:
 *                 type: string
 *                 format: date-time
 *                 description: 结束时间
 *               bannerImage:
 *                 type: string
 *                 description: 横幅图片URL
 *               rules:
 *                 type: string
 *                 description: 活动规则
 *               maxVotesPerUser:
 *                 type: integer
 *                 description: 每个用户最大投票数
 *     responses:
 *       200:
 *         description: 创建成功
 *       400:
 *         description: 请求参数错误
 *       401:
 *         description: 未授权
 *       409:
 *         description: 时间冲突
 */
router.post('/', [
  authenticate,
  authorize([ALL_PERMISSIONS.ACTIVITY_CREATE]),
  body('name').notEmpty().withMessage('活动名称不能为空'),
  body('startTime').isISO8601().withMessage('开始时间格式错误'),
  body('endTime').isISO8601().withMessage('结束时间格式错误'),
  body('maxVotesPerUser').optional().isInt({ min: 1 }).withMessage('最大投票数必须是正整数'),
  validateRequest
], activityController.createActivity);

/**
 * @swagger
 * /api/admin/activities:
 *   get:
 *     summary: 获取活动列表
 *     tags: [活动管理]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: query
 *         name: page
 *         schema:
 *           type: integer
 *         description: 页码
 *       - in: query
 *         name: limit
 *         schema:
 *           type: integer
 *         description: 每页数量
 *       - in: query
 *         name: status
 *         schema:
 *           type: string
 *           enum: [ACTIVE, INACTIVE, COMPLETED, CANCELLED]
 *         description: 活动状态
 *       - in: query
 *         name: search
 *         schema:
 *           type: string
 *         description: 搜索关键词
 *     responses:
 *       200:
 *         description: 获取成功
 *       401:
 *         description: 未授权
 */
router.get('/', [
  authenticate,
  authorize([ALL_PERMISSIONS.ACTIVITY_READ]),
  query('page').optional().isInt({ min: 1 }).withMessage('页码必须是正整数'),
  query('limit').optional().isInt({ min: 1, max: 100 }).withMessage('每页数量必须在1-100之间'),
  validateRequest
], activityController.getActivities);

/**
 * @swagger
 * /api/admin/activities/{id}:
 *   get:
 *     summary: 获取活动详情
 *     tags: [活动管理]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: integer
 *         description: 活动ID
 *     responses:
 *       200:
 *         description: 获取成功
 *       401:
 *         description: 未授权
 *       404:
 *         description: 活动不存在
 */
router.get('/:id', [
  authenticate,
  authorize([ALL_PERMISSIONS.ACTIVITY_READ]),
  param('id').isInt().withMessage('活动ID必须是整数'),
  validateRequest
], activityController.getActivity);

/**
 * @swagger
 * /api/admin/activities/{id}:
 *   put:
 *     summary: 更新活动
 *     tags: [活动管理]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: integer
 *         description: 活动ID
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             properties:
 *               name:
 *                 type: string
 *                 description: 活动名称
 *               description:
 *                 type: string
 *                 description: 活动描述
 *               startTime:
 *                 type: string
 *                 format: date-time
 *                 description: 开始时间
 *               endTime:
 *                 type: string
 *                 format: date-time
 *                 description: 结束时间
 *               status:
 *                 type: string
 *                 enum: [ACTIVE, INACTIVE, COMPLETED, CANCELLED]
 *                 description: 活动状态
 *               bannerImage:
 *                 type: string
 *                 description: 横幅图片URL
 *               rules:
 *                 type: string
 *                 description: 活动规则
 *               maxVotesPerUser:
 *                 type: integer
 *                 description: 每个用户最大投票数
 *     responses:
 *       200:
 *         description: 更新成功
 *       400:
 *         description: 请求参数错误
 *       401:
 *         description: 未授权
 *       404:
 *         description: 活动不存在
 *       409:
 *         description: 时间冲突
 */
router.put('/:id', [
  authenticate,
  authorize([ALL_PERMISSIONS.ACTIVITY_UPDATE]),
  param('id').isInt().withMessage('活动ID必须是整数'),
  body('startTime').optional().isISO8601().withMessage('开始时间格式错误'),
  body('endTime').optional().isISO8601().withMessage('结束时间格式错误'),
  body('maxVotesPerUser').optional().isInt({ min: 1 }).withMessage('最大投票数必须是正整数'),
  validateRequest
], activityController.updateActivity);

/**
 * @swagger
 * /api/admin/activities/{id}:
 *   delete:
 *     summary: 删除活动
 *     tags: [活动管理]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: integer
 *         description: 活动ID
 *     responses:
 *       200:
 *         description: 删除成功
 *       401:
 *         description: 未授权
 *       404:
 *         description: 活动不存在
 *       409:
 *         description: 活动已有投票数据
 */
router.delete('/:id', [
  authenticate,
  authorize([ALL_PERMISSIONS.ACTIVITY_DELETE]),
  param('id').isInt().withMessage('活动ID必须是整数'),
  validateRequest
], activityController.deleteActivity);

/**
 * @swagger
 * /api/admin/activities/{id}/artists:
 *   post:
 *     summary: 添加艺人到活动
 *     tags: [活动管理]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: integer
 *         description: 活动ID
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             required:
 *               - artistId
 *             properties:
 *               artistId:
 *                 type: integer
 *                 description: 艺人ID
 *               sortOrder:
 *                 type: integer
 *                 description: 排序顺序
 *     responses:
 *       200:
 *         description: 添加成功
 *       400:
 *         description: 请求参数错误
 *       401:
 *         description: 未授权
 *       404:
 *         description: 活动或艺人不存在
 *       409:
 *         description: 艺人已在活动中
 */
router.post('/:id/artists', [
  authenticate,
  authorize([ALL_PERMISSIONS.ACTIVITY_UPDATE]),
  param('id').isInt().withMessage('活动ID必须是整数'),
  body('artistId').isInt().withMessage('艺人ID必须是整数'),
  body('sortOrder').optional().isInt().withMessage('排序顺序必须是整数'),
  validateRequest
], activityController.addArtistToActivity);

/**
 * @swagger
 * /api/admin/activities/{activityId}/artists/{artistId}:
 *   delete:
 *     summary: 从活动移除艺人
 *     tags: [活动管理]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: activityId
 *         required: true
 *         schema:
 *           type: integer
 *         description: 活动ID
 *       - in: path
 *         name: artistId
 *         required: true
 *         schema:
 *           type: integer
 *         description: 艺人ID
 *     responses:
 *       200:
 *         description: 移除成功
 *       401:
 *         description: 未授权
 *       404:
 *         description: 活动或艺人不存在
 *       409:
 *         description: 艺人已有投票数据
 */
router.delete('/:activityId/artists/:artistId', [
  authenticate,
  authorize([ALL_PERMISSIONS.ACTIVITY_UPDATE]),
  param('activityId').isInt().withMessage('活动ID必须是整数'),
  param('artistId').isInt().withMessage('艺人ID必须是整数'),
  validateRequest
], activityController.removeArtistFromActivity);

/**
 * @swagger
 * /api/admin/activities/{id}/artists/order:
 *   put:
 *     summary: 更新活动艺人排序
 *     tags: [活动管理]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: integer
 *         description: 活动ID
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             required:
 *               - artists
 *             properties:
 *               artists:
 *                 type: array
 *                 items:
 *                   type: object
 *                   properties:
 *                     artistId:
 *                       type: integer
 *                       description: 艺人ID
 *                     sortOrder:
 *                       type: integer
 *                       description: 排序顺序
 *     responses:
 *       200:
 *         description: 排序更新成功
 *       400:
 *         description: 请求参数错误
 *       401:
 *         description: 未授权
 *       404:
 *         description: 活动不存在
 */
router.put('/:id/artists/order', [
  authenticate,
  authorize([ALL_PERMISSIONS.ACTIVITY_UPDATE]),
  param('id').isInt().withMessage('活动ID必须是整数'),
  body('artists').isArray().withMessage('艺人列表必须是数组'),
  body('artists.*.artistId').isInt().withMessage('艺人ID必须是整数'),
  body('artists.*.sortOrder').isInt().withMessage('排序顺序必须是整数'),
  validateRequest
], activityController.updateActivityArtistOrder);

/**
 * @swagger
 * /api/admin/activities/{id}/statistics:
 *   get:
 *     summary: 获取活动投票统计
 *     tags: [活动管理]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: integer
 *         description: 活动ID
 *     responses:
 *       200:
 *         description: 获取成功
 *       401:
 *         description: 未授权
 *       404:
 *         description: 活动不存在
 */
router.get('/:id/statistics', [
  authenticate,
  authorize([ALL_PERMISSIONS.ACTIVITY_READ]),
  param('id').isInt().withMessage('活动ID必须是整数'),
  validateRequest
], activityController.getActivityVoteStatistics);

export default router;
